<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //  ### promise相关题目
        //#### 第一题：执行顺序,写出执行逻辑 及原因

        setTimeout(() => {
            console.log(1)
        })
        new Promise((resolve, reject) => {
            console.log(2)
            resolve()
        }).then(() => {
            console.log(3)
        })
        console.log(4)

        //#### 第二题：写出执行顺序，写出执行逻辑及原因

        setTimeout(() => {
            console.log('set1')
            new Promise((resolve, reject) => {
                console.log('pr1')
                resolve()
            }).then(() => {
                console.log('then1')
            })
        })
        setTimeout(() => {
            console.log('set2')
        })
        new Promise((resolve, reject) => {
            console.log('pr2')
            resolve()
        }).then(() => {
            console.log('then2')
        })
        new Promise((resolve, reject) => {
            console.log('pr3')
            setTimeout(() => {
                console.log('set3')
            })
            resolve()
        }).then(() => {
            console.log('then3')
        })
        console.log(1)



        //#### 第三题

        setTimeout(() => {
            console.log('set1')
        }, 2000)
        setTimeout(() => {
            console.log('set2')
        }, 1000)
        new Promise((resolve, reject) => {
            console.log('pr1')
            setTimeout(() => {
                console.log('set3')
                resolve()
            }, 1000)
        }).then(() => {
            setTimeout(() => {
                console.log('set4 then1')
            }, 200)
        })



        // ### 第四题


        console.log('1');

        setTimeout(function () {
            console.log('2');
            new Promise(function (resolve) {
                console.log('3'); resolve();
            }).then(function () {
                console.log('4')
            })
        })
        new Promise(function (resolve) {
            console.log('5');
            resolve();
        }).then(function () {
            console.log('6')
        })
        setTimeout(function () {
            console.log('7');
            new Promise(function (resolve) {
                console.log('8');
                resolve();
            }).then(function () {
                console.log('9')
            })
        })



    </script>
</body>

</html>